package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.POIUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("/ordersetting")
@RestController
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 打印日志
     */
    private static final Logger log = LoggerFactory.getLogger(OrderSettingController.class);

    /**
     * 批量导入预约设置
     * @param excelFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile){

        try {
            //- 接收上传的文件，MultipartFile  参数名为excelFile
            //- 调用POIUtils解析excel，List<String[]>
            List<String[]> strings = POIUtils.readExcel(excelFile);

            //- 再把List<String[]> 转成List<OrderSetting>，日期格式化
            final SimpleDateFormat sdf = new SimpleDateFormat(POIUtils.DATE_FORMAT);

            List<OrderSetting> orderSettingList = strings.stream().map(orderInfoStringArr->{

                //新建预约设置
                OrderSetting os = new OrderSetting();

                // 日期的字符串
                String orderDataStr = orderInfoStringArr[0];

                try {
                    //将日期格式化
                    os.setOrderDate(sdf.parse(orderDataStr));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                // 设置最大预约数量
                os.setNumber(Integer.parseInt(orderInfoStringArr[1]));

                return os;
            }).collect(Collectors.toList());

            //- 调用服务层，导入(添加)预约设置
            orderSettingService.add(orderSettingList);

        } catch (IOException e) {
            e.printStackTrace();

            //打印异常日志
            log.error("导入预约设置失败",e);

            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

        //返回成功信息结果给界面
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 展示日历
     * @param mouth
     * @return
     */
    @GetMapping("/getDataByMonth")
    public Result getDataByMonth(String mouth){
        //调用服务层,获取日历
        List<Map<String,Integer>> monthData = orderSettingService.getDataByMonth(mouth);

        //将日历返回给前端界面
        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,monthData);
    }

    /**
     * 基于日历的预约设置
     * @return
     */
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){

        //调用服务层来更新
        orderSettingService.editNumberByDate(orderSetting);

        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }
}
